<?php

namespace App\Admin\Actions\Grid;

use App\Models\ProductTag;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class TagProductRemove extends RowAction
{
    /**
     * @return string
     */
	protected $title = '移除';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $product_id = $request->get('product_id');
        $tag_id = $this->getKey();
        $is_delete = ProductTag::where('tag_id',$tag_id)->where('product_id',$product_id)->delete();
        if($is_delete){
            return $this->response()
                ->success('移除成功')
                ->refresh();
        }else{
            return $this->response()
                ->error('该商品已被移除')->refresh();
        }

    }

    /**
	 * @return string|array|void
	 */
	public function confirm()
	{
		// return ['Confirm?', 'contents'];
	}

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [
            'product_id' => $this->row->product_id,
        ];
    }
}
